package com.xinke.module.system.service.oauth2;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.xinke.module.system.model.vo.oauth2.*;
import com.xinke.module.system.model.entity.oauth2.OAuth2ApproveDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.oauth2.OAuth2ApproveMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;

/**
 * OAuth2 批准 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class OAuth2ApproveServiceImpl implements OAuth2ApproveService {

    @Resource
    private OAuth2ApproveMapper oAuth2ApproveMapper;

    @Override
    public Long createOAuth2Approve(OAuth2ApproveSaveReqVO createReqVO) {
        // 插入
        OAuth2ApproveDO oAuth2Approve = BeanUtils.toBean(createReqVO, OAuth2ApproveDO.class);
        oAuth2ApproveMapper.insert(oAuth2Approve);
        // 返回
        return oAuth2Approve.getApproveId();
    }

    @Override
    public void updateOAuth2Approve(OAuth2ApproveSaveReqVO updateReqVO) {
        // 校验存在
        validateOAuth2ApproveExists(updateReqVO.getApproveId());
        // 更新
        OAuth2ApproveDO updateObj = BeanUtils.toBean(updateReqVO, OAuth2ApproveDO.class);
        oAuth2ApproveMapper.updateById(updateObj);
    }

    @Override
    public void deleteOAuth2Approve(Long approveId) {
        // 校验存在
        validateOAuth2ApproveExists(approveId);
        // 删除
        oAuth2ApproveMapper.deleteById(approveId);
    }

    private void validateOAuth2ApproveExists(Long approveId) {
        if (oAuth2ApproveMapper.selectById(approveId) == null) {
            throw exception(null);
        }
    }

    @Override
    public OAuth2ApproveDO getOAuth2Approve(Long approveId) {
        return oAuth2ApproveMapper.selectById(approveId);
    }

    @Override
    public PageResult<OAuth2ApproveDO> getOAuth2ApprovePage(OAuth2ApprovePageReqVO pageReqVO) {
        return oAuth2ApproveMapper.selectPage(pageReqVO);
    }

}